import time

import openpyxl
from domain.cellMap import CellMap, get_dict
from domain.cellResultMap import CellResultMap
from domain.totalResult import TotalResult

if __name__ == '__main__':
    # 获取列下标字典
    index_dict = get_dict('./列标记.xlsx', 'Sheet1')
    # 打开工作簿
    wb = openpyxl.load_workbook('LTE指标.xlsx', read_only=True)
    # 获取工作表
    ws = wb['Sheet1']
    # 时间
    time_old = time.time()
    # 统计结果
    total_result = TotalResult()

    # 读取单元格数据
    i: int = 1
    for row in ws.rows:
        if i == 1:
            i = 2
            continue
        i += 1
        # 获取行对象
        cell_map = CellMap(index_dict)
        # 解析成cell_map
        cell_map.set_row_map(row)
        # 数据计算
        cell_result_map = CellResultMap(cell_map)
        # 进行统计
        city = cell_map.city.value
        if city not in total_result.__dict__.keys():
            # 新增城市
            total_result[city] = CellResultMap(cell_map)
        for key in cell_result_map.__dict__:
            # 统计初始化
            if total_result[city][key] is None:
                total_result[city][key] = 0
            # 无效数据，不进行统计
            if cell_result_map[key] is None:
                continue
            total_result[city][key] += cell_result_map[key]
        # 行数据统计完毕
        total_cell_map: CellResultMap = total_result[city]
        # 低接通小区统计
        total_cell_map.calculate_low_connection_cell_count(cell_result_map)
        # 高掉线小区统计
        total_cell_map.calculate_high_dropout_cell_count(cell_result_map)
        # TODO 低接通,高掉线小区完成

        # 一个基站的数据统计完毕，该地区基站数量+1
        total_cell_map.add_node_count(1)
        total_cell_map.add_rrc_node_count(1, cell_map)

        print(f'行号：{i - 1}， {cell_map}')
        # if i == 20:
        #     break

    # 计算最终数据
    total_result.calculate_result()
    print(total_result)
    print(f'----------------用时： {time.time() - time_old}')
